package com.zkthink.ceres.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zkthink.base.R;
import com.zkthink.ceres.product.dao.ProductGroupMapper;
import com.zkthink.ceres.product.dao.ProductMapper;
import com.zkthink.ceres.product.dto.ProductGroupUpdateDTO;
import com.zkthink.ceres.product.entity.ProductGroup;
import com.zkthink.ceres.product.service.ProductGroupService;
import com.zkthink.base.service.SuperServiceImpl;

import com.zkthink.ceres.product.vo.ProductGroupVO;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.context.BaseContextHandler;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.dozer.DozerUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * 商品分组表
 * </p>
 * @author JustArgo
 * @date 2020-05-07
 */
@Slf4j
@AllArgsConstructor
@Service
public class ProductGroupServiceImpl extends SuperServiceImpl<ProductGroupMapper, ProductGroup> implements ProductGroupService {

    private ProductMapper productMapper;

    private StoreService storeService;

    private DozerUtils dozerUtils;

    @Override
    public R<Boolean> deleteProductGroup(ProductGroupUpdateDTO productGroupUpdateDTO) {

        Integer count = productMapper.selectCountByProductGroupId(productGroupUpdateDTO.getId());
        if(count>0){
            return R.fail("该分组已绑定商品，不能删除");
        }

        baseMapper.delProductGroup(productGroupUpdateDTO.getId());

        return R.success();
    }

    @Override
    public R<List<ProductGroupVO>> findProductGroupList(Long id) {
        if(id==null){
            return R.fail(-1,"参数错误");
        }
        String tenantCode = storeService.findTenantCodeByStoreId(id);
        QueryWrapper<ProductGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("tenant_code",tenantCode);
        wrapper.orderByDesc("create_time");
        List<ProductGroup> groupList = baseMapper.selectList(wrapper);
        return R.success(dozerUtils.mapList(groupList,ProductGroupVO.class));
    }
}
